<template>
  <el-dialog v-model="dialogVisible" :title="dialogTitle" width="800">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      label-width="100px"
      label-position="left"
    >
      <div style="padding-bottom: 3%; font-size: 18px; font-weight: bold; color: coral">
        请尽快完成线下转帐付款
      </div>
      <el-form-item label="开户行：" prop="depositBank">
        <span style="margin-right: 10px; font-weight: bold">{{ formData.depositBank }}</span>
        <el-icon v-copy="formData.depositBank"><CopyDocument /></el-icon>
      </el-form-item>
      <el-form-item label="户名：" prop="accountName">
        <span style="margin-right: 10px; font-weight: bold">{{ formData.accountName }}</span>
        <el-icon v-copy="formData.accountName"><CopyDocument /></el-icon>
      </el-form-item>
      <el-form-item label="账号：" prop="accountNumber">
        <span style="margin-right: 10px; font-weight: bold">{{ formData.accountNumber }}</span>
        <el-icon v-copy="formData.accountNumber"><CopyDocument /></el-icon>
      </el-form-item>
      <el-form-item label="汇款金额" prop="quote">
        <span style="margin-right: 10px; font-weight: bold">￥{{ formData.quote }}</span>
      </el-form-item>
      <!-- <el-form-item label="用途/备注/摘要栏:" prop="note" label-width="150px">
      </el-form-item> -->
      <div style="font-weight: bold">
        您的订单号&nbsp;&nbsp;<span style="color: dodgerblue">{{ formData.orderNum }}</span
        >&nbsp;&nbsp;，汇款时填写在用途/备注/摘要栏，便于快速核销款项
      </div>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="success" @click="submitForm">已支付</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
import { ElMessageBox } from 'element-plus'
import { CopyDocument } from '@element-plus/icons-vue'
import * as OrderApi from '@/api/order'
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('支付订单') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  depositBank: '招商银行股份有限公司无锡分行',
  accountName: '中科数测（无锡）科技有限公司',
  accountNumber: '110950891910402',
  quote: null,
  note: null,
  orderNum: null
})
const formRef = ref() // 表单 Ref
const message = useMessage() // 消息弹窗

/** 打开弹窗 */
const open = async (row) => {
  dialogVisible.value = true
  resetForm()
  if (row.orderNum) {
    formLoading.value = true
    try {
      formData.value.orderNum = row.orderNum
      formData.value.quote = row.quote
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 重置表单 */
const resetForm = () => {
  formData.value.quote = null
  formData.value.orderNum = null
}

const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调

/** 提交表单 */
const submitForm = async () => {
  // 支付的二次确认
  ElMessageBox.confirm('是否确认已支付订单？', '已支付', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      // 发起支付
      await OrderApi.payOrder({ orderNum: formData.value.orderNum })
      dialogVisible.value = false
      message.success('提交成功')
      // 发送操作成功的事件
      emit('success')
    })
    .catch((error) => {
      console.log(error)
    })
}
</script>
